package com.wooming.myserver.service.impl;

import com.wooming.myserver.dto.GameDataDTO;
import com.wooming.myserver.dto.JwtDTO;
import com.wooming.myserver.entity.User;
import com.wooming.myserver.mapper.GameDataMapper;
import com.wooming.myserver.mapper.UserMapper;
import com.wooming.myserver.security.JWT;
import com.wooming.myserver.security.PasswordEncryption;
import com.wooming.myserver.service.inter.GameInitService;
import com.wooming.myserver.service.inter.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户服务实现类
 *
 * @author Wooming
 */
@Service
public class UserServiceImpl implements UserService {
    // 依赖项
    private final UserMapper userMapper;
    private final GameDataMapper gameDataMapper;
    private final GameInitService  gameInitService;

    // 构造函数注入
    @Autowired
    public UserServiceImpl(
            UserMapper userMapper,
            GameDataMapper gameDataMapper,
            GameInitService gameInitService
    ) {
        this.userMapper = userMapper;
        this.gameDataMapper = gameDataMapper;
        this.gameInitService = gameInitService;
    }

    @Override
    public User findUser(String username) {
        return userMapper.findByName(username);
    }

    @Override
    public User findUser(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public String getJWT(User user) throws Exception {
        GameDataDTO gameData = gameDataMapper.getGameData(user.getId());
        if (gameData == null) throw new RuntimeException("用户游戏数据不存在");
        JwtDTO jwtDTO = new JwtDTO(
                user.getId(),
                user.getUsername(),
                gameData.getLevel(),
                user.getRole()
        );
        return JWT.generate(jwtDTO);
    }

    @Override
    public boolean register(String username, String password) throws Exception {
        if(userMapper.findByName(username) != null) {   // 用户名已存在
            return false;
        } else {
            // 密码加密
            String passwordHash = PasswordEncryption.encrypt(password);
            // 插入用户
            userMapper.insert(username,  passwordHash);
            // 初始化用户游戏数据
            return gameInitService.init(userMapper.findByName(username).getId());
        }
    }

    @Override
    public boolean changeInformation(User user) throws Exception {
        if (userMapper.findByName(user.getUsername()) != null &&    //  用户名已存在
                // 用户名未变也放行
                !userMapper.findById(user.getId()).getUsername().equals(user.getUsername())
        ) {
            return false;
        } else {
            User oldUser = userMapper.findById(user.getId());
            oldUser.setUsername(user.getUsername());
            if (!user.getPasswordHash().isEmpty()) {   // 密码不为空，则修改密码
                oldUser.setPasswordHash(PasswordEncryption.encrypt(user.getPasswordHash()));
            }
            userMapper.update(oldUser);
            return true;
        }
    }
}
